// pages/login/msgLogin.js
const app = getApp()
import { sendMsg, msgLogin } from '../../api/user'
import {
  lockSysUser
} from '../../api/member'
import utils from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    verifyCodeKey: '',
    lineShow: true,
    phoneNumber: '',
    isShow: true,
    vCode:'',
    second: 60,
    SHARE_TYPE:0,
    xuid:null,
    ivcode:'',
    avatar:'',
    nickname:'',
    realAuth:app.globalData.nameAvatr,
    codeFlag:false
  },
  onChooseAvatar(e){
    this.selectComponent('#uploadAvatar').uploadImg([e.detail.avatarUrl]);
  },
  getNickname: function (e){
    let params = {}
    params.nickname = e.detail.value
    this.setData({
      nickname: e.detail.value,
    })
  },
  metaClick(e){
    let params = {}
    params.avatar = e.detail.images[ e.detail.images.length -1 ]
    app.globalData.userInfo.avatar = e.detail.images[ e.detail.images.length -1 ]
    this.setData({
      avatar:e.detail.images[ e.detail.images.length -1 ],
    })
  },
  startLockSysUser() {
    lockSysUser({ mchId: app.ISV_ID, xuid: app.globalData.xuid ,shopId:app.globalData.shopId}).then(res => {
      console.log(res)
      wx.showToast({
        title: res.content,
        icon: 'none',
        duration: 2000
      })
    })
  },
  loginClick() {
      let _this = this;
      let username = 'mch_'+app.ISV_ID+'_'+this.data.phoneNumber;

    if(app.globalData.nameAvatr == true){
      // if(this.data.avatar == ""){
      //   wx.showToast({
      //     icon: 'none',
      //     title: '请上传头像',
      //   })
      //   return false
      // }
      if(this.data.nickname == ""){
        wx.showToast({
          icon: 'none',
          title: '请输入昵称',
        })
        return false
      }
    }


    msgLogin({
        nickname:this.data.nickname,
        avatar:this.data.avatar,
        mchId: app.ISV_ID,
        username: username,
        verifyCode: this.data.vCode,
        xuid:app.globalData.xuid,
        ivcode:this.data.ivcode
      }).then(res => {
        if (res.type == 'success') {
          const {
            userInfo,
            token
          } = res.data
          app.globalData.token = token
           app.globalData.userInfo = userInfo

          _this.startLockSysUser()
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      }).catch(error => {
        wx.showToast({
          title: error.content,
          icon: 'none',
          duration: 2000
        })
      })
  

  },
  inputInvitationNum(e) {
    this.data.ivcode = e.detail.value
    this.setData({
      ivcode: this.data.ivcode
    })
  },
  inputPhoneNum(e) {
    this.data.phoneNumber = e.detail.value
    this.setData({
      phoneNumber: this.data.phoneNumber
    })
  },
  
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              isShow: true,
            })
            resolve(setTimer)
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  verificationCode(e) {

    this.data.vCode = e.detail.value

  },
  verificationClick() {
    if (this.data.phoneNumber == "" || this.data.phoneNumber.length != 11 || !(/^((1))\d{10}$/.test(this.data.phoneNumber))) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.timer()
    sendMsg({
      username: this.data.phoneNumber,
            tplKey: 'register',
      mchId: app.ISV_ID,
    }).then(res => {

    })
    this.data.isShow = false
    this.setData({
      isShow: this.data.isShow
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.xuid=app.xuid
    this.setData({
      xuid:this.data.xuid
    })
  },

})
